package J11_17;

import java.util.Scanner;

public class test {
    //给定一个整数sum，从有N个有序元素的数组中寻找元素a，b，使得a+b的结果最接近sum
    void selectNumber(int[] a,int sum){
        int[] arr = {1,2,3,4,5,6,7,8};
        for (int i = 0; i < arr.length; i++) {
            for (int j = arr.length-1; j > i; j--) {
                if(arr[i] + arr[j] == sum){
                    a[0] = arr[i];
                    a[1] = arr[j];
                }
            }
        }
    }

    //给出𝑛(𝑛 = 1)个整数𝑎i (0 ≤ 𝑎i ≤ 106 ) 和𝑚(1 ≤ 𝑚 ≤ 105 )对整数𝑙i , 𝑟𝑖 (0 ≤ 𝑙𝑖 ≤ 𝑟𝑖 ≤ 106 )表示𝑚个区间 请问每一个区间包含的𝑎𝑖的个数的总量是多少？ 形式化的说，对于所有的𝑖,𝑗 (𝑖 = 1, 1 ≤ 𝑗 ≤ 𝑚)，有多少对满足𝑙𝑗 ≤ 𝑎𝑖 ≤ 𝑟𝑗 ?

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int m = scanner.nextInt();

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }

        int[] l = new int[m];
        int[] r = new int[m];
        for (int i = 0; i < m; i++) {
            l[i] = scanner.nextInt();
            r[i] = scanner.nextInt();
        }

        int result = countInInterval(n, a, m, l, r);
        System.out.println(result);
    }

    public static int countInInterval(int n, int[] a, int m, int[] l, int[] r) {
        int[] cnt = new int[n + 1];
        for (int i = 0; i < m; i++) {
            cnt[l[i]] += 1;
            cnt[r[i] + 1] -= 1;
        }

        int ans = 0;
        for (int i = 1; i <= n; i++) {
            cnt[i] += cnt[i - 1];
            ans += cnt[i];
        }

        return ans;
    }
        public static void main1(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int m = in.nextInt();
            int a = in.nextInt();
            int[][] arr =  new int[m][2];
                for(int i = 0;i<m;i++){
                    for(int j = 0;j<2;j++){
                        arr[i][j] = in.nextInt();
                    }
                }
            int b = 0;
            for(int i = 0;i<m-1;i++){
                if(arr[i][0]<=a && arr[i][1]>=a){
                    b++;
                }
            }
            System.out.print(b);
            in.close();
        }
    }
